package cn.kow.kmall.common.wxpay;

import cn.kow.kmall.common.enums.KmallResultEnum;
import cn.kow.kmall.config.WXAccountConfig;
import cn.kow.kmall.pojo.entity.Order;
import cn.kow.kmall.core.exception.ServiceException;
import cn.kow.kmall.core.util.JsonUtils;
import cn.kow.kmall.core.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-08-16 19:25
 * @Modified By:
 */
@Slf4j
public class WXPayHelper {

    public static Map<String, String> unifiedOrder(HttpServletRequest request, String openid, Order order, WXAccountConfig wxPayConfig)
            throws Exception {
        String orderNo = order.getOrderNo();

        // 金额检查
        Integer payAmount = order.getPayFee();
        if (payAmount < 0) {
            throw new ServiceException(KmallResultEnum.ORDER_AMOUNT_ERROR);
        }
        // 发起支付
        WXPay wxPay = new WXPay(wxPayConfig);
        Map<String, String> data = new HashMap<>();
        data.put("body", "寻木居");
        data.put("out_trade_no", orderNo);
        data.put("fee_type", "CNY");
        data.put("total_fee", String.valueOf(payAmount));
        data.put("spbill_create_ip", WXPayUtil.getIpAddress(request));
        data.put("notify_url", wxPayConfig.getNotifyUrl());
        data.put("trade_type", "JSAPI");
        data.put("openid", openid);
        Map<String, String> resp = wxPay.unifiedOrder(data);
        log.info("调用统一下单接口，resp={}", JsonUtils.toJson(resp));
        if (WXPayConstants.FAIL.equals(resp.get("return_code"))) {
            throw new ServiceException(KmallResultEnum.PAY_FAIL);
        }

        String timeStamp = String.valueOf(WXPayUtil.getCurrentTimestamp());
        String nonceStr = UUIDUtils.get32UUID();
        String signType = "MD5";
        String packAge = "prepay_id=" + resp.get("prepay_id");

        Map<String, String> wxPayMap = new HashMap<String, String>();
        wxPayMap.put("appId", wxPayConfig.getAppID());
        wxPayMap.put("timeStamp", timeStamp);
        wxPayMap.put("nonceStr", nonceStr);
        wxPayMap.put("package", packAge);
        wxPayMap.put("signType", signType);

        String sign = WXPayUtil.generateSignature(wxPayMap, wxPayConfig.getKey());

        Map<String, String> payResponse = new HashMap<>();
        payResponse.put("nonceStr", nonceStr);
        payResponse.put("package", packAge);
        payResponse.put("signType", signType);
        payResponse.put("timeStamp", timeStamp);
        payResponse.put("paySign", sign);

        log.info("参数加密，payResponse={}", JsonUtils.toJson(payResponse));
        return payResponse;
    }

    public static Map<String,String> refund(String orderNo, String refundNo, Integer amount, WXAccountConfig wxPayConfig)
            throws Exception {
        WXPay wxPay = new WXPay(wxPayConfig);
        amount = 1;
        Map<String,String> reqData = new HashMap<>();
//        reqData.put("transaction_id", tradeNo);
        reqData.put("out_trade_no", orderNo);
        reqData.put("out_refund_no", refundNo);
        reqData.put("total_fee", String.valueOf(amount));
        reqData.put("refund_fee", String.valueOf(amount));
//        reqData.put("notify_url", "https://mgg.gzqqs.com/gumiho/wxc/refund/notify");
        Map<String,String> repData = wxPay.refund(reqData);
        return repData;
    }
}
